package com.youlai.system.common.util;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.youlai.system.security.util.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

/**
 * @version 1.0
 * @Author zzps
 * @Date 2023/9/5 22:58
 * @注释 自动填充时间、创建人等
 */
@Component
public class MyMetaHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        setFieldValByName("createdTime", new Date(), metaObject);
        setFieldValByName("modifiedTime", new Date(), metaObject);
        setFieldValByName("createdUser", SecurityUtils.getUser() == null ? null : SecurityUtils.getUser().getUsername(), metaObject);
        setFieldValByName("modifiedUser", SecurityUtils.getUser() == null ? null : SecurityUtils.getUser().getUsername(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        setFieldValByName("modifiedTime", new Date(), metaObject);
        setFieldValByName("modifiedUser", SecurityUtils.getUser() == null ? null : SecurityUtils.getUser().getUsername(), metaObject);
    }
}
